<?php

namespace model;

class videos
{
	const TABLE = "video";
	const TABLE_CATE = "video_cate";
	const R_ID = ROUTE['query']['id'];
	private static function db($table = null)
	{
		return \libs\db::Init($table);
	}

	//关联查找一条数据
	public static function findData()
	{
		$where['id'] = self::R_ID;
		return self::db(self::TABLE)->where($where)->find();
	}
	//更新浏览次数据  +1
	public static function hitsData()
	{
		$where['id'] = self::R_ID;
		return self::db(self::TABLE)->where($where)->Update(['hits' => '{{hits + 1}}']);
	}

	//查询所有栏目列表
	protected static function getCateData()
	{
		return self::db(self::TABLE_CATE)->Cache(false)->Order('sort ASC')->Select();
	}

	//根据已经查询到的数组，查找指定的一条栏目数据
	public static function findCateData($cid)
	{
		return FindOneArrayBYFilter(self::getCateData(), 'id', $cid, ['name', 'theme', 'id', 'num']);
	}

	//查询 GET值 栏目分页列表  支持分页缓存->cache(600)
	public static function listData($cateVal)
	{
		$result['cate'] = self::findCateData($cateVal); //查询所在栏目数据
		$where['cid'] = $result['cate']['id'];
		$where['status'] = 1;
		$p = ROUTE['query']['p'];
		$num = $result['cate']['num'] ?? 25;  //每页显示条数
		$page = ['num' => $num, 'p' => $p, 'return' => ['prev', 'next', 'first', 'last', 'list']];
		$result['data'] = self::db(self::TABLE)->where($where)->page($page)->order('id DESC')->select();
		$result['page'] = self::db(self::TABLE)->getPage();
		return $result;
	}


	/**
	 *  根据条件 联表 查询指定内容列表  并查出当前所属栏目信息
	 *	$cid = 栏目ID   $limit  = 1,10条数,  $cache  = 缓存时间 秒
	 **/
	public static function selectJoinData($cid, $limit, $cache)
	{
		$where['a.cid'] = $cid;
		$where['a.status'] = 1;
		$field = 'b.id as cateid , b.name as catename ,a.id,a.cid,a.title,a.status,a.time';
		$join = 'LEFT JOIN video_cate b ON a.cid=b.id';
		return self::db(self::TABLE . ' a')->field($field)->Join($join)->cache($cache)->where($where)->limit($limit)->select();
	}


	/**
	 *  根据条件 查询指定内容列表
	 *	$cid = 栏目ID
	 *	$limit  = 1,10条数,
	 *	$cache  = 缓存时间 秒
	 *	使用方法: 在模板位置  循环 foreach(\model\article::selectData(7,5,60) as $vo){ echo $vo['title']; }
	 **/
	public static function selectData($cid = 0, $limit = 10, $cache = 600, $imgbl = 0, $att = 0, $order = "id DESC")
	{
		if ($cid == 0) $where = "status = 1";
		else $where = "cid IN (" . $cid . ") AND status = 1";
		if ($imgbl == 1) $where .= " AND imgbl = 1";
		if ($att == 1) $where .= " AND att_a = 1";
		if ($att == 2) $where .= " AND att_b = 1";
		if ($att == 3) $where .= " AND att_c = 1";
		return self::db(self::TABLE)->cache($cache)->where($where)->limit($limit)->order($order)->select();
	}
}
